define(['angular',
        'underscore',
        'config.properties',
        'jquery'],
    function (angualr, _, config) {
        var CONSTANTS = {
            RANGE_TYPE: {
                TWO_HOURS: 'TWO_HOURS',
                ONE_DAY: 'ONE_DAY',
                ONE_WEEK: 'ONE_WEEK',
                ONE_MONTH: 'ONE_MONTH',
                CUSTOM: 'CUSTOM',
                FIXED: 'FIXED'
            }
        };
        return angular.module('DateRangerModule', []).directive('gDateRanger', function ($parse) {
            return {
                template: ' <div id="each"><li class="time-selection-l on" ng-click="select(\'ONE_DAY\')" style="padding: 5px 16px 4px 16px"><a href="javascript:void(0)"  >最近一天</a></li><li class="time-selection-c"  ng-click="select(\'ONE_WEEK\')" ><a href="javascript:void(0)"  >  一周内</a></li><li class="time-selection-r" ng-click="select(\'ONE_MONTH\')"> <a href="javascript:void(0)"  >一月内</a></li></div> ',
                replace: true,
                restrict: 'E',
                transclude: false,
                scope: {
                    buttonClick: "&"
                },
                require: 'ngModel',
                link: function (scope, element, attrs, ngModelController) {
                    var lickTime = 0;
                    scope.isCustom = false;
                    scope.showCustomBnt = false;
                    scope.select = function (rangeType) {
                        scope.rangeType = rangeType;
                        if (rangeType === CONSTANTS.RANGE_TYPE.FIXED) {
                            scope.rangeType = CONSTANTS.RANGE_TYPE.TWO_HOURS;
                        }
                        ngModelController.$setViewValue({rangeType: rangeType});
                        scope.start = "";
                        scope.end = "";
                        if (lickTime > 0 && rangeType !== CONSTANTS.RANGE_TYPE.FIXED && rangeType !== CONSTANTS.RANGE_TYPE.CUSTOM) {
                            scope.buttonClick();
                        }
                        lickTime = lickTime + 1;
                    };

                    scope.$watch("start", function () {
                        if (scope.rangeType === CONSTANTS.RANGE_TYPE.CUSTOM) {
                            ngModelController.$setViewValue({
                                rangeType: scope.rangeType,
                                start: scope.start,
                                end: scope.end
                            })
                        }
                    });

                    scope.$watch("end", function () {
                        if (scope.rangeType === CONSTANTS.RANGE_TYPE.CUSTOM) {
                            ngModelController.$setViewValue({
                                rangeType: scope.rangeType,
                                start: scope.start,
                                end: scope.end
                            })
                        }
                    });
                    

                    var defRangeType = $parse('controls.dateRanger.defRangeType')(config) || CONSTANTS.RANGE_TYPE.TWO_HOURS;
                    scope.select(defRangeType);
                }
            };
        });
    });